package com.zhengshi.formal;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class fanzhuan {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		// **************反转帐号，唯一更改****************
		String wenjian = "帐号整理";
		String filepath = "E:\\"+wenjian.trim()+".txt";

		// **************反转帐号，唯一更改****************
		String filepath2 = "E:\\ZhangHao/Copy.txt";
		String filepath1 = "E:\\";// F:\VDownload\腾讯
		int bytesum = 0;
		int byteread = 0;
		File oldfile = new File(filepath);
		if (oldfile.exists()) { // 文件存在时
			InputStream inStream;
			try {
				inStream = new FileInputStream(filepath);
				// 读入原文件
				FileOutputStream fs = new FileOutputStream(filepath2);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; // 字节数 文件大小
					System.out.println(bytesum);
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
			} catch (Exception e) {
				System.out.println("复制帐号文件操作出错");
				e.printStackTrace();
			}
		}

		ArrayList<String> list = new ArrayList<String>();
		SimpleDateFormat format0 = new SimpleDateFormat("yyyy年MM月dd日  HH时mm分ss秒");
		Date ss = new Date();
		String time0 = format0.format(ss.getTime());
		try {
			for (int i = 1; i < 99999; i++) {
				InputStreamReader reader;

				reader = new InputStreamReader(new FileInputStream(filepath2));
				BufferedReader br = new BufferedReader(reader);
				String line1 = "";
				line1 = br.readLine();
				if (line1==null) {
					break;
				}
				System.out.println("第" + i + "个帐号读取成功:" + line1);
				list.add(line1);
				// 删除第一行
				List<String> list2 = readFile(filepath2, line1);
				writeFile(filepath2, list2);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Collections.reverse(list);
		for (String zh : list) {
			method2(filepath1 + "/账号反转" + time0 + ".txt", zh);
		}
		System.out.println("帐号反转完毕");
	}

	// 方法1：

	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	// 方法3：

	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static List<String> readFile(String filePath, String str) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			if (!text.trim().equals(str)) {
				list.add(text + "\r\n");
			}
			text = reader.readLine();
		}
		reader.close();
		return list;
	}

	public static void writeFile(String filePath, List<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write(s.getBytes());
		}
		outputStream.close();
	}
}
